package com.helloandroid.paso.ui.crewlist;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;

	//TODO: NEW! mRelR RelativeLayout make nullpointer exeption, because not setted. Method getCrewList must have a RelativeLayout parameter to do this... 

public class CrewListView{
	private ListView mListOfMembers;
	private ArrayAdapter<String> mListAdapter;
	private boolean click;
	private int mlist_custom_text, mlist_content;
	private Context mContext;
	private RelativeLayout mRelR, mWebRel;
	private WebView mwebVHold;
  
	String[] mmembers, mcrew, mprevmembers, mlinks;
	
    /**  	mCrewlist.getCrewList(getResources().getStringArray(R.array.members_array), getResources().getStringArray(R.array.crew_array),
						getResources().getStringArray(R.array.prevmembers_array), getResources().getStringArray(R.array.memberslink_array), (ListView) findViewById(R.id.ListOfMembers),
						(Button) findViewById(R.id.MembersButton), (Button) findViewById(R.id.CrewButton), (Button) findViewById(R.id.PrevMembersButton), R.layout.list_custom_text, R.id.list_content,
						(WebView) findViewById(R.id.detailWebview), getApplicationContext(), (RelativeLayout) findViewById(R.id.CrewList_ListHolder), (RelativeLayout) findViewById(R.id.WebWiewLayout),
						(Button) findViewById(R.id.BackButton)); */
	

    public void getCrewList(String[] members, String[] crew, String[] prevmembers, String[] links, 
    		ListView listOfMembers, Button mMembersButton, Button mCrewButton, Button mPrevMembersButton,
    		int list_custom_text, int list_content,WebView webView, Context context, RelativeLayout relR, RelativeLayout webRel, Button mBackButton){

    	mmembers=members;
    	mcrew = crew;
    	mprevmembers = prevmembers;
    	mlist_custom_text = list_custom_text;
    	mlist_content = list_content;
    	mListOfMembers = listOfMembers;
    	mContext = context;
    	mlinks = links;
    	mwebVHold = webView;
    	mRelR = relR;
    	mWebRel = webRel;
    	
    	mListAdapter = new ArrayAdapter<String>(mContext, mlist_custom_text, mlist_content, mmembers);
		mListOfMembers.setAdapter(mListAdapter);
		click = true;
        
    	mMembersButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				
				mListAdapter = new ArrayAdapter<String>(mContext, mlist_custom_text, mlist_content, mmembers);
				mListOfMembers.setAdapter(mListAdapter);
				click = true;
				mRelR.setVisibility(RelativeLayout.VISIBLE);
				mWebRel.setVisibility(RelativeLayout.GONE);
			}
		});
    	
    	mCrewButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mListAdapter = new ArrayAdapter<String>(mContext, mlist_custom_text, mlist_content, mcrew);
				mListOfMembers.setAdapter(mListAdapter);
				click = false;
				mWebRel.setVisibility(RelativeLayout.GONE);
				mRelR.setVisibility(RelativeLayout.VISIBLE);
			}
		});
    	
    	mPrevMembersButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mListAdapter = new ArrayAdapter<String>(mContext, mlist_custom_text, mlist_content, mprevmembers);
				mListOfMembers.setAdapter(mListAdapter);
				click = false;
			}
		});
    	
    	mBackButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mWebRel.setVisibility(RelativeLayout.GONE);
				mRelR.setVisibility(RelativeLayout.VISIBLE);
			}
		});
    	
    	
    	mListOfMembers.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int itemClicked,long arg3) {
				if(click){
				//	Detail d = new Detail();
					mWebRel.setVisibility(RelativeLayout.VISIBLE);
					mRelR.setVisibility(RelativeLayout.GONE);
					getDetail( mwebVHold, mlinks[itemClicked], mContext);
					
					
				}			
			}
		});   	
    }
public void getDetail(WebView wView, String url, Context c) {
		
		wView.setWebViewClient(new WebViewClient()); 
		
		wView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {

				//if (progress == 100)
					//pB.setVisibility(View.GONE);
			}
		});
		wView.loadUrl(url);
	}
}